package com.lkm.app_ui.ui.fj.img;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.log.AppLogger;
import com.lkm.app_ui.m.camera.CameraModule;
import com.lkm.app_ui.ui.fj.FjOrder;
import com.lkm.javatool.lang.RunnablePR;
import com.lkm.market2_0.entity.UpDataApiTo;



public class FjImgTask  {

    private CameraHandler handler;
    private static final int SLIENT_TACK_CAMERA = 1;
    private static final int SLIENT_CLOSE_CAMERA = 2;

    public FjImgTask(CameraModule cameraModule) {
        handler = new CameraHandler(cameraModule);

        
        UpImageTask.tryUp();
    }



    public void requestTackImg(FjOrder.FjItem fjItem) {
        UpDataApiTo upDataApiTo = RunTimeData.steelyardInfo.upDataApiTo;
        
        if (upDataApiTo != null && upDataApiTo.isUpImage())
            handler.sendMessage(handler.obtainMessage(SLIENT_TACK_CAMERA, new Object[]{fjItem, true}));

    }



    private static class CameraHandler extends Handler {
        CameraModule cameraModule;
        
        Boolean preGoodEntityIshold = null;
        
        volatile FjOrder.FjItem preGoodEntity = null;

        CameraHandler(CameraModule cameraModule) {
            super(Looper.getMainLooper());
            this.cameraModule = cameraModule;
        }

        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case SLIENT_TACK_CAMERA:

                    if (!cameraModule.isOpen()) {
                        cameraModule.open();
                    }
                    if (cameraModule.isOpen()) {
                        FjOrder.FjItem goodEntity_ = (FjOrder.FjItem) ((Object[]) msg.obj)[0];
                        boolean isHold_ = (boolean) ((Object[]) msg.obj)[1];


                        AppLogger.Order.tackPictureStart();

                        int status = cameraModule.tackPicture(new RunnablePR<byte[], Void>() {
                            @Override
                            public Void run(byte[] data) {
                                AppLogger.Order.tackPictureOk();
                                FjOrder.FjItem goodEntity = preGoodEntity;
                                preGoodEntity = null;
                                if (data == null || goodEntity == null) return null;
                                ImgFiler.ayncSave(goodEntity, data);
                                return null;
                            }
                        });
                        if (status == 0) {
                            
                            preGoodEntityIshold = isHold_;
                            preGoodEntity = goodEntity_;
                        } else if (status == 2) {

                            
                            if (preGoodEntityIshold != null)
                                if (!preGoodEntityIshold && preGoodEntity != null) {
                                    preGoodEntity = goodEntity_;
                                    preGoodEntityIshold = isHold_;
                                }
                        }
                        AppLogger.Order.tackPictureIng();

                    }
                    break;
                case SLIENT_CLOSE_CAMERA: {
                    cameraModule.close();
                }
                break;
                default:
                    break;
            }
        }
    }

}
